package com.hj.dapp.business.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.dapp.business.project.mapper.ProjectMapper;
import com.hj.dapp.business.project.model.Project;
import com.hj.dapp.business.project.service.ProjectService;
import com.hj.dapp.common.request.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/24
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public void save(Project project, Principal principal) {
        project.setCreateBy(principal.getName());
        project.setCreateTime(LocalDateTime.now());
        projectMapper.insert(project);
    }

    @Override
    public Project get(Long id) {
        return projectMapper.selectById(id);
    }

    @Override
    public IPage<Project> page(PageRequest<Project> pageRequest) {
        Page<Project> page = new Page<>();
        page.setCurrent(pageRequest.getCurrent());
        page.setSize(pageRequest.getSize());
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(pageRequest.getQuery())) {
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getName())) {
                queryWrapper.like("name", pageRequest.getQuery().getName());
            }
        }
        return projectMapper.selectPage(page,queryWrapper);
    }

    @Override
    public void delete(Long id) {
        projectMapper.deleteById(id);
    }

    @Override
    public List<Project> list() {
        return projectMapper.selectList(null);

    }

    @Override
    public void edit(Project project, Principal principal) {
        project.setLastUpdateBy(principal.getName());
        project.setLastUpdateTime(LocalDateTime.now());
        projectMapper.updateById(project);
    }
}
